/* =============================================================
 * System Name：Document Control System
 * File Name： QQApi.java
 * --------------------------------------------------------------
 * (C) Copyright M+W Group 2012 All Rights Reserved.
 *
 *  VERSION    DATE        BY                CHANGE/COMMENT
 * ----------------------------------------------------------------
 *
 */
package com.deco.authorization.api;

import java.net.URLEncoder;

import org.scribe.builder.api.DefaultApi10a;
import org.scribe.model.OAuthConfig;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuth10aServiceImpl;
import org.scribe.oauth.OAuthService;

public class QQApi extends DefaultApi10a {

	private static final String AUTHORIZE_URL = "http://openapi.qzone.qq.com/oauth/qzoneoauth_authorize?oauth_token=%s&oauth_consumer_key=%s&oauth_callback=%s";
	private static final String REQUEST_TOKEN_RESOURCE = "openapi.qzone.qq.com/oauth/qzoneoauth_request_token";
	private static final String ACCESS_TOKEN_RESOURCE = "openapi.qzone.qq.com/oauth/qzoneoauth_access_token";
	// due to the restriction of sina oauth api, we have to put the callback url
	// here.
	private String CALLBACK_URL;
	private String appId;

	@Override
	public String getAccessTokenEndpoint() {
		return "http://" + String.format(ACCESS_TOKEN_RESOURCE,appId);
	}

	@Override
	public String getRequestTokenEndpoint() {
		return "http://" + REQUEST_TOKEN_RESOURCE;
	}

	@Override
	public String getAuthorizationUrl(Token requestToken) {
		return String.format(AUTHORIZE_URL, requestToken.getToken(),appId,
				URLEncoder.encode(CALLBACK_URL));
	}

	public void setCallbackURL(String callbackURL) {
		CALLBACK_URL = callbackURL;
	}
	

	public void setAppId(String appId) {
		this.appId = appId;
	}

	public Verb getAccessTokenVerb(){
		return Verb.GET;
	}

	public Verb getRequestTokenVerb(){
		return Verb.GET;
	}

	@Override
	public OAuthService createService(OAuthConfig config) {
	    return new OAuth10aServiceQQZoneImpl(this, config);
	}
	
	

}